import {login, logout, permissionMenus} from "../api/star_horse";
import {removeToken, setToken, setUserInfo} from "../utils/auth";

const actions = {
    /**
     * 缓存Tab数据
     * @param commit
     * @param data
     */
    commitNavBar({commit}, data) {
        commit("setNavData", data);
        commit("setViewCache", data);
    },
    /**
     * 清除Tab数据
     * @param commit
     * @param data
     */
    clearNavBar({commit}, data) {
        commit("clearNavItem", data);
    },
    /**
     * 清除缓存
     * @param commit
     * @param data
     */
    clearCache({commit}, data) {
        commit("clearCache", data);
    },
    /**
     * 用户登录
     * @param commit
     * @param data
     */
    loginAction({commit}, data) {
        let that = this;
        return new Promise((resolve, reject) => {
            login(data).then(res => {
                let redata = res.data;
                let userData = redata.data;
                if (redata.code === 500) {
                    reject(redata.cnMessage);
                } else {
                    let condition = {
                        "userId": userData.idAdmin,
                    };
                    setToken(redata.token, data.rememberMe);
                    setUserInfo(userData);
                    commit("loginMutation", userData);
                    permissionMenus(condition).then(res1 => {
                        let menuDatas = res1.data.data;
                        if (res1.data.code === 500) {
                            reject(menuDatas.cnMessage);
                        } else {
                            // commit("permissionMenusMutation", menuDatas);
                            userData.rolesList=[];
                            resolve();
                        }
                    }).catch(error => {
                        console.log("内部异常", error)
                        reject(error);
                    });
                }

            }).catch(error => {
                console.log("登录异常")
                reject(error);
            });
        });

    },
    permissionOperation({commit}, data) {
        let condition = {
            "userId": data.idUsersinfo,
        };
        return new Promise((resolve, reject) => {
            permissionMenus(condition).then(res1 => {
                let menuDatas = res1.data.data;
                commit("permissionMenusMutation", menuDatas);
                resolve();
            }).catch(error => {
                console.log("内部异常", error)
                reject(error);
            });
        });

    },
    getMenus({rootGetters}) {
        return new Promise((resolve, reject) => {
            // console.log(rootGetters.getPermissionMenus);
            resolve(rootGetters.getPermissionMenus);
        })
    },
    /**
     * 退出登录
     * @param commit
     * @param data
     */
    logOut({commit}, data) {
        return new Promise((resolve, reject) => {
            logout(data).then(res => {
                let redata = res.data;
                if (redata.code === 1) {
                    reject(redata.cnMessage);
                } else {
                    removeToken()
                    commit("logoutMutation", data);
                    resolve();
                }
            }).catch(error => reject(error));
        });
    },
    /**
     * 切换Tab
     * @param commit
     * @param data
     */
    changeTab({commit}, data) {
        commit("changeTabMutation", data);
    }
}
export default actions;